import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {
  ElementPlusResolver,
  VueUseComponentsResolver,
  VueUseDirectiveResolver,
} from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import compressPlugin from 'vite-plugin-compression'

const resolve = dir => path.join(__dirname, dir)
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), 'VITE')
  // const proxyUrl = `${env.VITE_BASE_PROTOCOL}://${env.VITE_BASE_API}`
  return {
    plugins: [
      vue(),
      Components({
        dts: true,
        dirs: ['src/components'], // 按需加载的文件夹
        extensions: ['vue', 'js'],
        resolvers: [
          ElementPlusResolver({ importStyle: 'sass' }),
          VueUseComponentsResolver(),
          VueUseDirectiveResolver(),
          IconsResolver({
            prefix: 'icon',
            enabledCollections: ['ep'],
          }),
        ],
        eslintrc: {
          enabled: true,
        },
        // 允许子目录作为组件的命名空间前缀
        directoryAsNamespace: true,
        // 忽略命名空间前缀的子目录路径
        globalNamespaces: ['components'],
      }),
      Icons({
        // compiler: "vue3",
        // 自动安装
        autoInstall: true,
        // 自定义图标加载
        customCollections: {
          // 给svg文件设置fill="currentColor"属性，使图标的颜色具有适应性
          // home: FileSystemIconLoader('src/assets/svg/home', svg => svg.replace(/^<svg /, '<svg fill="currentColor" ')),
        },
      }),
      AutoImport({
        include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/],
        dts: true,
        eslintrc: {
          enabled: true,
        },
        imports: ['vue', 'vue-router', 'pinia'],
        resolvers: [
          ElementPlusResolver({ importStyle: 'sass' }),
          // 自动导入图标组件
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
      }),
      // 压缩
      compressPlugin({
        ext: '.gz',
        threshold: 0,
        filter: () => true,
        deleteOriginFile: false,
      }),
    ],
    define: {
      'process.env': {
        ...env,
        command,
        mode,
      },
    },
    resolve: {
      alias: {
        '@': resolve('src'),
        '@components': resolve('src/components'),
        '@api': resolve('src/apis'),
        '@views': resolve('src/views'),
        '@styles': resolve('src/styles'),
        '@images': resolve('src/assets/images'),
        '@store': resolve('src/store'),
        '@utils': resolve('src/utils'),
        '@hooks': resolve('src/hooks'),
      },
      // 省略文件后缀
      extensions: ['', '.js', '.json', '.vue', '.scss', '.css'],
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/elementVar.scss" as *;`,
        },
      },
    },
    server: {
      host: '0.0.0.0',
      port: 8082,
      proxy: {},
    },
  }
})
